package com.kk.goGame.mapper.impl;
/*
 *@author KK on 2023.02.14 19:18
 */

import com.kk.goGame.constant.Constant;
import com.kk.goGame.entity.User;
import com.kk.goGame.mapper.UserMapper;
import com.qf.framwork.base.BaseDao;

import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMapperImpl extends BaseDao<User> implements UserMapper {
    @Override
    public Integer insertUser(User user) {
        String sql = "insert into user value (default,?,?,?,?,?,?,?,?,default,default,default,default ) ";
        System.out.println(user.getHeader());
        return super.insert(sql,
                user.getUsername(),
                user.getPassword(),
                user.getSex(),
                user.getNickname(),
                user.getHeader(),
                user.getEmail(),
                user.getPhone(),
                user.getScore());
    }

    @Override
    public User checkColumn(String column, Object value) {
        String sql = Constant.USER_SQL + "where " + column + " = ? ";
        return (User) super.queryOne(sql, User.class, value);
    }

    @Override
    public int updatePassword(String newpassword,String username) {
        String sql = "update user set password = ? where username = ?";
        int i = super.update(sql, newpassword,username);
        return i;
    }

    /**
     * 修改对应的用户积分
     * @param uid
     * @param score
     * @return
     */
    @Override
    public Integer updateScore(Integer uid, Integer score) {
        String sql = "update user set score = score + ?, update_time = now() where id = ?";
        return super.update(sql, score, uid);
    }

    @Override
    public Map<String,Integer> tongjiSex() {
        Map<String,Integer> sexMap = new ConcurrentHashMap<>();
        String sql="SELECT COUNT(id) AS 'value',(CASE WHEN sex=0 THEN 'women' WHEN sex=1 THEN 'men' END)AS 'name' FROM USER GROUP BY sex";
        return super.query(sql, resultSet -> {
            while (resultSet.next()){
                sexMap.put(resultSet.getString("name"),resultSet.getInt("value"));
            }
            return sexMap;
        });
    }

    @Override
    public List<User> tongjiScore() {
        String sql = "SELECT username,score FROM USER ORDER BY score DESC";
        List<User> users = super.queryList(sql,User.class);
        return users;
    }


}
